package com.etc.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;

/**
 * Servlet implementation class VCodeServlet
 */
@WebServlet("/vcode.do")
public class VCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream  outputstream = response.getOutputStream();
		//定义图形验证码的长、宽、验证码字符数、干扰线宽度
		ICaptcha captcha = CaptchaUtil.createShearCaptcha(300, 100, 4, 4);
		//拿到code 打印输出
		String code = captcha.getCode();
		//将code存储到servletContext
		request.getServletContext().setAttribute("code", code);
		System.out.println(code);
		
		captcha.write(outputstream);
		//Servlet的OutputStream记得自行关闭哦！
		outputstream.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
